import { createStore } from 'vuex';

import { INCREMENT_N } from './mutation-types'

import axios from 'axios'
const store = createStore({
    state() {
        return {
            counter: 1,
            name: "yzh",
            age: 18,
            height: 1.88,
            books: [
                {name: "深入Vuejs", price: 200, count:3},
                {name: "深入javascript", price: 199, count:7},
                {name: "深入React", price: 100, count:2},
                {name: "深入Node", price: 300, count:5},
            ],
            discount: 0.5,  //1.折扣
            banners: []
        }
    },
    mutations: {
        increment(state) {
            state.counter++
        },
        decrement(state) {
            state.counter--
        },
        //使用常量名
        [INCREMENT_N](state, payload) {
            // console.log(payload);
            
            state.counter += payload.n
        },
        setBanners(state, payload) {
            state.banners = payload
        }
    },
    getters: {
        totalPrice(state, getters) {
            let totalPrice = 0;
            for (const book of state.books) {
                totalPrice += book.price * book.count
            }
            return (totalPrice * getters.currentDiscount).toFixed(0)  //3.调用另一个计算属性
        },
        currentDiscount(state) {
            return state.discount * 0.9 //2.折上折
        },
        totalCountN(state, getters) {
            return function (n) { //返回一个匿名函数
                let totalPrice = 0;
                for (const k of state.books) {
                    if (k.count > n) {
                        totalPrice += k.price * k.count
                    }
                }
                return (totalPrice * getters.currentDiscount).toFixed(0)
            }
        },
        nameInfo(state) {
            return `name: ${state.name}`
        },
        ageInfo(state) {
            return `age: ${state.age}`
        },
        heightInfo(state) {
            return `height: ${state.height}`
        }
    },
    actions: {
        incrementAction(context) {
            setTimeout(() => {
                context.commit("increment")
            }, 1000)
        },
        decrementAction({commit, state, getters}) { //解构
            commit("decrement")            
        },
        // getHomeBanner(context) {
        //     axios.get("http://123.207.32.32:8000/home/multidata").then(res => {
        //         //触发方法 方法赋值
        //         context.commit("setBanners", res.data.data.banner.list)
        //     })
        // }
        getHomeBanner(context) {
            //返回一个 promise
            return new Promise((resolve, reject) => {
                axios.get("http://123.207.32.32:8000/home/multidata").then(res => {
                    context.commit("setBanners", res.data.data.banner.list)
                    resolve({naem: "yzh", age:"18"})
                }).catch(err => {
                    reject(err)
                })
            })
            
        }
    }
});

export default store